package com.qpl.depression.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.entity.Category;
import com.qpl.depression.entity.CategoryLevel;
import com.qpl.depression.entity.Paper;
import com.qpl.depression.service.CategoryLevelService;
import com.qpl.depression.service.CategoryService;
import com.qpl.depression.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryLevelService categoryLevelService;
    Long startTime;
    /**
     * 测试试题页面
     *
     * @param id       试题分类id
     * @param modelMap 试题数据
     * @return 试题页面
     */
    @GetMapping("paper/page/{id}")
    public String examPage(@PathVariable("id") Long id, ModelMap modelMap) {
        List<Paper> paper = paperService.list(Wrappers.<Paper>lambdaQuery().eq(Paper::getStatus, 1).eq(Paper::getCategoryId, id));
        modelMap.addAttribute("papers", paper);
        modelMap.addAttribute("paperSize", paper.size());
        startTime = System.currentTimeMillis();
        return "paper";
    }

    /**
     * 拿到测试结果
     *
     * @param answer     传递过来的答案
     * @param categoryId 分类id
     * @param map        存放数据
     * @return
     */
    @GetMapping("paper/detail")
    public String paperDetail(String answer, Long categoryId, ModelMap map) {

        List<Paper> papers = paperService.list(Wrappers.<Paper>lambdaQuery().eq(Paper::getCategoryId, categoryId).eq(Paper::getStatus, 1));
        Category category = categoryService.getById(categoryId);
        map.addAttribute("paperSize", papers.size());
        BigDecimal scope = BigDecimal.valueOf(getScope(papers, answer, category));
        map.addAttribute("scope", scope);
        // 根据分数比较建议
        CategoryLevel categoryLevel = categoryLevelService.getByScope(categoryId, scope).get(0);
        map.addAttribute("categoryLevel", categoryLevel);
        map.addAttribute("time", (System.currentTimeMillis() - startTime)/ 1000/ 60);
        map.addAttribute("beginTime", startTime);
        return "paperDetail";
    }

    /**
     * 计算分数值
     *
     * @param list
     * @param answer
     * @param category
     * @return
     */
    public double getScope(List<Paper> list, String answer, Category category) {
        // 初始化分数
        double scope = 0.0;
        int firstIndex = answer.indexOf(",");
        String newString = answer.substring(firstIndex + 1);
        String[] strings = newString.split(",");
        // 初始化用户提交的答案
        List answers = new ArrayList();
        for (String s : strings) {
            answers.add(s);
        }
        // 先取出来正确的答案
        List trueList = new ArrayList();
        for (Paper trueAnswer : list) {
            trueList.add(trueAnswer.getTrueAnswer());

        }
        // 计算每一题的分数值
        double signAnswer = Double.parseDouble(category.getScope()) / list.size();
        // 和填写的答案进行比较
        for (int i = 0; i < list.size(); i++) {
            if (answers.get(i).equals(trueList.get(i))) {
                scope = scope + signAnswer;
            }
        }

        return scope;
    }

}
